package com.sy.live.main.thread.football;

import java.sql.Timestamp;

import org.apache.log4j.Logger;

import com.sy.live.entity.TbVideoLink;
import com.sy.live.entity.constant.CategoryConstant;
import com.sy.live.entity.constant.HttpConstant;
import com.sy.live.main.thread.nba.GetLinkByNBA98;
import com.sy.live.service.ILinkService;
import com.sy.live.util.DateUtil;
import com.sy.live.util.GetIp;
import com.sy.live.util.HttpUtil;
import com.sy.live.util.SpringBeans;
import com.sy.live.util.XMLConfigUtil;

public class GetLinkByTencent extends Thread
{
	private static long millis = Long.parseLong(XMLConfigUtil.getSysProperty("thread.GetLinkByTencent")) * 1000 * 60L;
	private static Logger logger = Logger.getLogger(GetLinkByNBA98.class);
	private static String uri = "http://sports.qq.com/isocce/fcc2013/cfsc/guide.htm";
	private static ILinkService linkService = SpringBeans.getBeanByType(ILinkService.class);

	@Override
	public void run()
	{
		while (true)
		{
			// 获得联合会杯主方法
			getLinkAndSaveToDB();

			try
			{
				sleep(millis);
			} catch (InterruptedException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * 获得联合会杯主方法
	 */
	private void getLinkAndSaveToDB()
	{
		// 通过GET方法获得网页内容
		String body = HttpUtil.getResponseBodyByGetMethod(uri, HttpConstant.GB2312);

		if (body == null || body.trim().equals(""))
		{
			logger.info("扫描网页返回内容为空.");
			return;
		} else
		{
			String line[] = body.split("\n");

			for (int i = 0; i < line.length; i++)
			{
				String superLink = "";
				String content = "";
				boolean flag = false;
				
				String t = line[i];
				String detail = "";
					
				if(t.contains("月") && t.contains("日"))
				{
					detail = "<tr>";
					
					String dateStr = t.substring(t.indexOf("月") - 2, t.indexOf("日") + 1);
				
					for(int j = i + 1; j < line.length; j++)
					{
						t = line[j];
						detail += t.trim();
						
						i++;
						
						if(t.contains("</tr>"))
						{
							break;
						}
						
						if(t.contains("："))
						{
							//加上年份和时间
							dateStr = DateUtil.getIntYearNow() + "年" + dateStr + t.replaceAll("：", "时").replaceAll("<td>", "").replaceAll("</td>", "").trim() + "分";
						}else if(t.contains("<a"))
						{
							if(t.contains("class"))
							{
								//获得内容
								content = t.substring(t.indexOf("stitle") + 8, t.indexOf("btime") - 2);
							}else if (!t.contains("style="))
							{
								//获得链接
								superLink = t.substring(t.indexOf("href") + 6, t.indexOf("target") - 2);
							}
						}
						
						flag = true;
					}
					
					if(flag)
					{
						//通过内容和子类ID判断是否存在该记录
						TbVideoLink link = linkService.findRecordByContentAndCategoryId(CategoryConstant.FOOTBALL, content);
						
						if(link != null)
						{
							if(!link.getContent().equals(content))
							{
								link.setContent(content);
								link.setDetail(t.trim());
								
								linkService.updateEntity(link);

								logger.info("视频日期[" + dateStr + "];链接地址[" + superLink + "];链接内容[" + content + "]的记录更新成功.");
							}
							
							logger.info("视频日期[" + dateStr + "];链接地址[" + superLink + "];链接内容[" + content + "]的记录已经添加.");
							
							continue;
						}else
						{
							Timestamp currentTimestamp = new Timestamp(System.currentTimeMillis());
							
							link = new TbVideoLink();
							
							link.setAddTime(currentTimestamp);
							link.setContent(content);
							link.setUpdateTime(currentTimestamp);
							link.setDetail(detail.trim());
							link.setPlayTime(new Timestamp(DateUtil.parseChinaLongDate(dateStr).getTime()));
							link.setSuperLink(superLink);
							link.setVisited(0);
							link.setAddIp(GetIp.getLocalIp());
							link.setCategoryId(CategoryConstant.FOOTBALL);
							
							//添加记录
							linkService.addEntity(link);
							
							logger.info("视频日期[" + dateStr + "];链接地址[" + superLink + "];链接内容[" + content + "]的记录添加成功.");
						}
					}
				}
			}
		}
	}
}
